package sys.spvisor.core.service.project;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import sys.spvisor.core.dao.ana.TRoleMapper;
import sys.spvisor.core.dao.ana.TUserRoleMapper;
import sys.spvisor.core.dao.project.TProjectPeopleMapper;
import sys.spvisor.core.dao.project.TPushMesssageMapper;
import sys.spvisor.core.entity.ana.TRole;
import sys.spvisor.core.entity.ana.TRoleExample;
import sys.spvisor.core.entity.ana.TRoleExample.Criteria;
import sys.spvisor.core.entity.ana.TUserRole;
import sys.spvisor.core.entity.ana.TUserRoleExample;
import sys.spvisor.core.entity.people.TPeople;
import sys.spvisor.core.entity.project.PushMessageCustom;
import sys.spvisor.core.entity.project.TProjectPeople;
import sys.spvisor.core.entity.project.TProjectPeopleExample;
import sys.spvisor.core.entity.project.TPushMesssage;
import sys.spvisor.core.entity.project.TPushMesssageExample;
import sys.spvisor.core.service.people.PeopleService;

@Service
public class PushMessageService {
	@Autowired
	TPushMesssageMapper tPushMesssageMapper;
	@Autowired
	TRoleMapper tRoleMapper;
	@Autowired
	TUserRoleMapper tUserRoleMapper;
	@Autowired
	TProjectPeopleMapper tProjectPeopleMapper;
	@Autowired
	PeopleService peopleService;
	@Autowired
	ProjectService projectService;

	//发送消息
	//参数，第一个项目id，默认为0，role，推送人员的角色，message，推送的信息，sendpeoId，推送人的id，sendPeoName，推送人的姓名,fileMap,推送的文件
	@Transactional
	public void sendMessage(PushMessageCustom custom) {
		//不在项目中的角色，不能用proId进行判断，用角色进行判断
		if(custom.getRole() != "项目经理" || custom.getRole() != "项目总监" ||custom.getRole() != "监理组长"  ||custom.getRole() != "监理员" ) {
			//根据role查找在角色表中找到对应的人员
			TRoleExample tRoleExample = new TRoleExample();
			Criteria criteria = tRoleExample.createCriteria();
			criteria.andRoleDescEqualTo(custom.getRole());
			//查找对用的角色，只有一个角色id
			TRole tRole = tRoleMapper.selectByExample(tRoleExample).get(0);
			TUserRoleExample tUserRoleExample = new TUserRoleExample();
			sys.spvisor.core.entity.ana.TUserRoleExample.Criteria criteria2 = tUserRoleExample.createCriteria();
			//根据角色id查找对应的与角色对应的用户
			criteria2.andRoleIdEqualTo(tRole.getRoleId());
			List<TUserRole> lists1 = tUserRoleMapper.selectByExample(tUserRoleExample);
			//找到用户角色表，根据用户角色表中的userid字段，对其推送消息
			for (TUserRole tUserRole : lists1) {
				TPeople people = peopleService.getPeopleById(tUserRole.getRoleId());
				TPushMesssage tPushMesssage = new TPushMesssage();
				tPushMesssage.setUserId(tUserRole.getUserId());
				tPushMesssage.setUserName(people.getPeoName());
				tPushMesssage.setMsgContent(custom.getMessage());
				tPushMesssage.setPushSendUserId(custom.getSendpeoId());
				tPushMesssage.setPushSendUserName(custom.getSendPeoName());
				tPushMesssage.setFileFileId(custom.getFileId());
				tPushMesssage.setFileName(custom.getFileName());
				tPushMesssage.setFileProId(custom.getProId());
				//判断所在项目是否为空
				if(custom.getProId() == 0) {
					tPushMesssage.setFileProName("");
				}else {
					tPushMesssage.setFileProName(projectService.getProjectNameBymsgId(custom.getProId()));
				}
				if(tPushMesssage.getFileName().equals(null) || tPushMesssage.equals("")) {
					//这个需要进行改进，有文件是未审核，没有文件是未读
					tPushMesssage.setCheckStatus("未审核");
					//这个也需要进行改进，如果是文件类型的消息，那么就没有未通过，
					//疑问，也可以在消息读取的时候标志成通过
					tPushMesssage.setCheckResult("未通过");
				} else {
					tPushMesssage.setCheckStatus("未读");
				}
				
				tPushMesssage.setPushTime(new Date(new java.util.Date().getTime()));
				//往推送消息列表总插入消息
				tPushMesssageMapper.insert(tPushMesssage);
				System.out.println("推送消息成功1");
			}
		} else {
			//这里是在项目中的角色
			TProjectPeopleExample tProjectPeopleExample = new TProjectPeopleExample();
			sys.spvisor.core.entity.project.TProjectPeopleExample.Criteria criteria = tProjectPeopleExample.createCriteria();
			criteria.andPpMsgidEqualTo(custom.getProId());
			criteria.andPpProRoleDescEqualTo(custom.getRole());
			List<TProjectPeople> tProjectPeoples = tProjectPeopleMapper.selectByExample(tProjectPeopleExample);
			for (TProjectPeople tProjectPeople : tProjectPeoples) {
				TPushMesssage tPushMesssage = new TPushMesssage();
				tPushMesssage.setUserId(tProjectPeople.getPpPeopleid());
				tPushMesssage.setUserName(tProjectPeople.getPpUsername());
				tPushMesssage.setMsgContent(custom.getMessage());
				tPushMesssage.setPushSendUserId(custom.getSendpeoId());
				tPushMesssage.setPushSendUserName(custom.getSendPeoName());
				tPushMesssage.setFileFileId(custom.getFileId());
				tPushMesssage.setFileName(custom.getFileName());
				tPushMesssage.setFileProId(custom.getProId());
				//判断所在项目是否为空
				if(custom.getProId() == 0) {
					tPushMesssage.setFileProName("");
				}else {
					tPushMesssage.setFileProName(projectService.getProjectNameBymsgId(custom.getProId()));
				}
				if(tPushMesssage.getFileName().equals(null) || tPushMesssage.equals("")) {
					//这个需要进行改进，有文件是未审核，没有文件是未读
					tPushMesssage.setCheckStatus("未审核");
					//这个也需要进行改进，如果是文件类型的消息，那么就没有未通过，
					//疑问，也可以在消息读取的时候标志成通过
					tPushMesssage.setCheckResult("未通过");
				} else {
					tPushMesssage.setCheckStatus("未读");
				}
				tPushMesssage.setPushTime(new Date(new java.util.Date().getTime()));
				//往推送消息列表总插入消息
				tPushMesssageMapper.insert(tPushMesssage);
				System.out.println("推送消息成功2");
			}
			
		}
	}
	
	//接收消息
	//第一个参数，表示用户的id,第二个参数表示状态，0表示未处理，1表示已经处理，2表示已处理未处理都查询
	@Transactional
	public List<TPushMesssage> getMessage(int userId,int status) {
		TPushMesssageExample tPushMesssageExample = new TPushMesssageExample();
		sys.spvisor.core.entity.project.TPushMesssageExample.Criteria criteria = tPushMesssageExample.createCriteria();
		List<String> unchecked = new ArrayList<String>();
		unchecked.add("未审核");
		unchecked.add("未读");
		List<String> checked = new ArrayList<String>();
		checked.add("已审核");
		checked.add("已读");
		criteria.andUserIdEqualTo(userId);
		if(status == 0) {
			criteria.andCheckStatusIn(unchecked);
		} else if(status == 1) {
			criteria.andCheckStatusIn(checked);
		} else if(status == 2) {
			
		}
		List<TPushMesssage> lists = tPushMesssageMapper.selectByExample(tPushMesssageExample);
		return lists;
	}
	
	
	//status，审核通过还是不通过，两种类型，一个是通过，一个是未通过
	public int updatePushMessage(int pushMessageId,String status) {
		int rows = 0;
		TPushMesssage messsage = tPushMesssageMapper.selectByPrimaryKey(pushMessageId);
		if(messsage.getFileName().equals(null) || messsage.getFileName() == "") {
			if(status.equals("通过") || status == "通过") {
				messsage.setCheckStatus("已读");
			} else {
				//如果是普通消息，就不用修改消息内容了
				rows = 1;
			}
			
		} else {
			if(status.equals("通过") || status == "通过") {
				messsage.setCheckStatus("已审核");
				messsage.setCheckResult("通过");
			} else {
				//TODO
				/*
				 * 这个地方需要改进，如果未通过，需要给发送人发送未通过的消息
				 */
				messsage.setCheckStatus("已审核");
				messsage.setCheckResult("未通过");
			}
			
		}
		rows = tPushMesssageMapper.updateByPrimaryKey(messsage);
		return rows;
		
	}
}










































